/**
 * @description: 检查文件是否存在
 * @return {name}输入的文件名
 * @author: lg
 */
const path = require('path');
const fs = require('fs-extra');
const inquirer = require('inquirer');
module.exports = async (name, options) => {
  // 判断项目是否存在
  const cwd = process.cwd();
  const targetAir = path.join(cwd, name);
  // 目录是否存在
  if (fs.existsSync(targetAir)) {
    // 是否为强制创建
    if (options.force) {
      await fs.rmSync(targetAir, { recursive: true });
    } else {
      const { action } = await inquirer.prompt([
        {
          name: 'action',
          type: 'list',
          message: 'Target directory already exists',
          choices: [
            {
              name: 'Overwrite',
              value: 'overwrite'
            },
            {
              name: 'Cancel',
              value: false
            }
          ]
        }
      ]);

      // 如果用户拒绝覆盖则停止生于操作
      if (!action) {
        return true;
      } else if (action === 'overwrite') {
        await fs.rmSync(targetAir, { recursive: true });
      }
    }
  }
};
